package com.mall.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mall.commons.utils.R;
import com.mall.domain.*;
import com.mall.interface1.api.*;
import com.mall.vo.UmsAdminPageVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UmsRoleService {
    @DubboReference
    private UmsRoleMenuRelationApi umsRoleMenuRelationApi;
    @DubboReference
    private UmsRoleResourceApi umsRoleResourceApi;
    @DubboReference
    private UmsRoleApi umsRoleApi;
    @DubboReference
    private UmsMenuApi umsMenuApi;
    @DubboReference
    private UmsResourceApi umsResourceApi;
    public R allocMenu(Integer[] menuIds, Integer roleId) {
       umsRoleMenuRelationApi.save(menuIds, roleId);
       return R.success("",null);
    }

    public R allocResource(Integer[] resourceIds, Integer roleId) {
         umsRoleResourceApi.save(resourceIds, roleId);
        return R.success("",null);
    }

    public R create(UmsRole role) {
        umsRoleApi.save(role);
        return R.success("",null);
    }

    public R delete(Integer[] ids) {
       List<UmsRoleMenuRelation> list1=umsRoleMenuRelationApi.listIds(ids);
       List<UmsRoleResourceRelation> list2=umsRoleResourceApi.listIds(ids);
       if(CollUtil.isEmpty(list1)&&CollUtil.isEmpty(list2)){
           umsRoleApi.deleteIds(ids);
       }
       if(CollUtil.isNotEmpty(list1)){
           List<Long> menuIds = CollUtil.getFieldValues(list1, "menuId", Long.class);
           umsMenuApi.delete(menuIds);
       }
       if(CollUtil.isNotEmpty(list2)){
           List<Long> resourceIds = CollUtil.getFieldValues(list2, "resourceId", Long.class);
           umsResourceApi.deleteIds(resourceIds);
       }
       umsRoleApi.deleteIds(ids);
       return R.success("",null);
    }

    public R list(String keyword, Integer pageNum, Integer pageSize) {
        IPage<UmsRole> iPage=umsRoleApi.page(keyword, pageNum, pageSize);
        UmsAdminPageVo<UmsRole> vo = new UmsAdminPageVo<>();
        vo.setList(iPage.getRecords());
        vo.setTotal((int) iPage.getTotal());
        vo.setTotalPage((int) iPage.getPages());
        vo.setPageSize(pageSize);
        vo.setPageNum(pageNum);
        return R.success("",vo);
    }

    public R listAll() {
        List<UmsRole> list=umsRoleApi.findAll();
        return R.success("",list);
    }

    public R listMenu(Integer roleId) {
     List<UmsRoleMenuRelation> list=umsRoleMenuRelationApi.findById(roleId);
        List<Long> menuIds = CollUtil.getFieldValues(list, "menuId", Long.class);
        List<UmsMenu> menus=umsMenuApi.findByIds(menuIds);
        return R.success("",menus);
    }

    public R listResource(Integer roleId) {
     List<UmsRoleResourceRelation> list= umsRoleResourceApi.findById(roleId);
        List<Long> resourceIds = CollUtil.getFieldValues(list, "resourceId", Long.class);
        List<UmsResource> resources=umsResourceApi.findByIds(resourceIds);
        return R.success("",resources);
    }

    public R update(UmsRole role) {
        umsRoleApi.update(role);
        return R.success("",null);
    }

    public R updateStatus(Integer status, Integer id) {
     UmsRole umsRole=umsRoleApi.findById(id);
     if(umsRole!=null){
         umsRole.setStatus(status);
         umsRoleApi.update(umsRole);
     }
     return R.success("",null);
    }
}
